///////////////////////////////////////////////////////////////////////////////
//版权所有（C）2002-2022，成都梦想凯德科技有限公司。
//本软件代码及其文档和相关资料归成都梦想凯德科技有限公司
//应用包含本软件的程序必须包括以下声明
//在版权声明中：
//此应用程序与成都梦想凯德科技有限公司成协议。
//通过使用本软件、其文档或相关材料
///////////////////////////////////////////////////////////////////////////////

import { addAnimation } from "@/mapbox/animate";
import resourceTracker from "@/mxdraw/ResourceTracker";
import { MxFun } from "mxdraw";
import * as THREE from "three"
import { AnimateType } from "./types";
import { randomPonit } from "./utils";
// 光波纹理图片
const lighttextureImg = ""

interface MxThreeFluctuationsApertureOption {
    redius?: number
    color?:number | string | THREE.Color
    speed?: number
}
const texture = new THREE.TextureLoader().load(lighttextureImg)
// 波动光圈
export class MxThreeFluctuationsAperture extends THREE.Mesh {
    declare userData : {
        animate: AnimateType
        [x:string]:any;
    }
    constructor({ redius = 10000, color = 0xffffff * Math.random(), speed =  0.02 }:MxThreeFluctuationsApertureOption) {
        // 纹理
        const geometry = new THREE.PlaneBufferGeometry(redius * 2, redius * 2, 1, 1);
        const material = new THREE.MeshBasicMaterial({
            transparent: true,
            map: texture,
            side: THREE.DoubleSide,
            color: new THREE.Color(color),
            // 加载透明图片需要
            alphaTest: 0.1,
            depthWrite: true
        })
        super(geometry, material)
        const clock = new THREE.Clock()
        let time = 0
        this.userData.isStart = false
        this.userData.animate = (() => {
            if (!this.userData.isStart) return
            time += speed;
            this.scale.set(time, time, time);
            material.opacity = 1 - (time - 1) / 1.5; //缩放2.5对应0 缩放1.0对应1
            if (time > 2.5) time = 0;
            return this.userData.animate;
        }) as unknown as AnimateType
       
        this.userData.animate.start = () => {
            clock.start()
            this.userData.isStart = true
            return this.userData.animate
        }
        this.userData.animate.stop = () => {
            clock.stop()
            this.userData.isStart = false
            return this.userData.animate
        }
        
    }
}


export default function () {
    const draw = MxFun.getCurrentDraw()
    for (let i = 0; i < 100; i++) {
        const aperture = new MxThreeFluctuationsAperture({})
        aperture.position.copy(randomPonit())
        aperture.userData.animate.start()
        addAnimation('MxThreeFluctuationsAperture_i_'+ i, aperture.userData.animate)
        draw.addObject(aperture)
    }
}